package index;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import shopping.Order;
import java.io.IOException;
import java.util.List;

@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {

    // 处理GET请求（显示订单）
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession();

        String username = (String) session.getAttribute("username");
        if (username == null) {
            response.sendRedirect("loginRegister.jsp");
            return;
        }

        // 获取用户ID并查询订单
        int userId = JdbcUtils.getUserIdByUsername(username);
        List<Order> orders = JdbcUtils.getOrdersByUser(userId);

        request.setAttribute("orders", orders);
        request.getRequestDispatcher("orderManagement.jsp").forward(request, response);
    }

    // 处理POST请求（订单操作）
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");

        if (username == null) {
            response.sendRedirect("loginRegister.jsp");
            return;
        }
        int userId = JdbcUtils.getUserIdByUsername(username);

        switch (action) {
            case "add":
                String dishName = request.getParameter("dishName");
                double price = Double.parseDouble(request.getParameter("price"));
                String quantityParam = request.getParameter("quantity");
                int quantity = (quantityParam == null || quantityParam.isEmpty()) ? 1 : Integer.parseInt(quantityParam);

                if (JdbcUtils.insertOrder(userId, dishName, price, quantity)) {
                    response.sendRedirect("OrderServlet?status=added");
                } else {
                    request.setAttribute("error", "添加订单失败");
                    request.getRequestDispatcher("error.jsp").forward(request, response);
                }
                break;

            case "delete":
                int orderId = Integer.parseInt(request.getParameter("orderId"));
                if (JdbcUtils.deleteOrder(orderId, userId)) {
                    response.sendRedirect("OrderServlet?status=deleted");
                } else {
                    request.setAttribute("error", "删除订单失败");
                    request.getRequestDispatcher("error.jsp").forward(request, response);
                }
                break;

            case "update":
                int updateId = Integer.parseInt(request.getParameter("orderId"));
                int newQuantity = Integer.parseInt(request.getParameter("quantity"));
                if (JdbcUtils.updateOrderQuantity(updateId, userId, newQuantity)) {
                    response.sendRedirect("OrderServlet?status=updated");
                } else {
                    request.setAttribute("error", "更新数量失败");
                    request.getRequestDispatcher("error.jsp").forward(request, response);
                }
                break;
        }
    }
}
